package cn.example.demo.modules.house.service.impl;

import cn.example.demo.common.model.service.ServiceResult;
import cn.example.demo.common.retrieval.PageBean;
import cn.example.demo.common.secure.context.LoginUserApi;
import cn.example.demo.common.tools.QueryServiceUtils;
import cn.example.demo.common.tools.obj.reflect.EntityUtils;
import cn.example.demo.modules.house.dto.IndexCarouselDto;
import cn.example.demo.modules.house.entity.IndexCarousel;
import cn.example.demo.modules.house.mapper.IndexCarouselMapper;
import cn.example.demo.modules.house.service.IIndexCarouselService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 描述：【首页轮播】接口实现类
 */

@Service
public class IndexCarouselServiceImpl extends ServiceImpl<IndexCarouselMapper, IndexCarousel> implements IIndexCarouselService {
    @Autowired
    private LoginUserApi loginUserApi;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult insertIndexCarousel(IndexCarouselDto indexCarouselDto) {
        IndexCarousel entity = EntityUtils.entityConvert(indexCarouselDto, new IndexCarousel(), false);

        if (this.save(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【首页轮播，ID:{}】创建成功!", entity.getId()), entity);
        }

        return ServiceResult.isInternalError("【首页轮播】创建失败，未知错误！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult updateIndexCarousel(IndexCarouselDto indexCarouselDto) {
        IndexCarousel entity = this.getById(indexCarouselDto.getId());
        if (entity == null) {
            return ServiceResult.isNotFound(StrUtil.format("【首页轮播】信息更新失败：记录不存在！ID:{}", indexCarouselDto.getId()));
        }

        entity = EntityUtils.entityConvert(indexCarouselDto, entity, true);
        if (this.updateById(entity)) {
            return ServiceResult.isSuccess(StrUtil.format("【首页轮播，ID:{}】信息更新成功!", entity.getId()), null);
        }

        return ServiceResult.isNotModified("未知错误，【首页轮播】信息修改失败！");
    }

    @Override
    public PageBean queryIndexCarousel(IndexCarouselDto indexCarouselDto) {
        Page pageResult = PageHelper.startPage(indexCarouselDto.getPageNum(), indexCarouselDto.getPageSize())
                .doSelectPage(() -> this.list(getQueryWrapper(indexCarouselDto)));

        return QueryServiceUtils.encapsulatePageBean(new PageBean<>(), pageResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServiceResult deleteIndexCarousel(IndexCarouselDto indexCarouselDto) {
        int rows = this.baseMapper.deleteBatchIds(indexCarouselDto.getIds());
        if (rows > 0) {
            return ServiceResult.isSuccess(StrUtil.format("操作成功，已批量删除【首页轮播】共 {} 条记录！", rows), rows);
        }

        return ServiceResult.isNotModified("删除【首页轮播】失败，请稍后重试！");
    }

    @Override
    public IndexCarousel findIndexCarouselById(String id) {
        return this.getById(id);
    }

    @Override
    public List<Map<String, Object>> queryIndexCarouselAsDict(IndexCarouselDto indexCarouselDto) {
        return this.baseMapper.selectAsDictList(getQueryWrapper(indexCarouselDto));
    }

    /**
     * 查询条件
     *
     * @param indexCarouselDto
     * @return
     */

    protected LambdaQueryWrapper getQueryWrapper(IndexCarouselDto indexCarouselDto) {
        LambdaQueryWrapper<IndexCarousel> wrapper = Wrappers.lambdaQuery();

        wrapper
                .eq(StrUtil.isNotBlank(indexCarouselDto.getId()), IndexCarousel::getId, indexCarouselDto.getId())

                .like(StrUtil.isNotBlank(indexCarouselDto.getName()), IndexCarousel::getName, indexCarouselDto.getName())

                .like(StrUtil.isNotBlank(indexCarouselDto.getImageUrl()), IndexCarousel::getImageUrl, indexCarouselDto.getImageUrl())

                .eq(indexCarouselDto.getSort() != null, IndexCarousel::getSort, indexCarouselDto.getSort())

                .eq(indexCarouselDto.getImageStatus() != null, IndexCarousel::getImageStatus, indexCarouselDto.getImageStatus())
        ;

        wrapper.orderByAsc(IndexCarousel::getSort);

        return wrapper;
    }
}
